package com.qf.shiro;

import com.qf.pojo.User;
import com.qf.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class MyRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

//    md5加密
//        {
//            HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
//            matcher.setHashIterations(1024);
//            matcher.setHashAlgorithmName("MD5");
//            this.setCredentialsMatcher(matcher);
//
//        }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        UsernamePasswordToken param= (UsernamePasswordToken) token;
        User user = userService.findByName(param.getUsername());
        if (user==null){
            return null;
        }

//        SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(user, user.getPassword(), this.getName());
//        info.setCredentialsSalt(ByteSource.Util.bytes(user.getSalt()));

        return new SimpleAuthenticationInfo(user,user.getPassword(),"");

//        return new SimpleAccount(user,user.getPassword(),"");

    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("执行权限");

//        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//        Subject subject = SecurityUtils.getSubject();
//        User user = (User) subject.getPrincipal();
//        User byName = userService.findByName(user.getName());
//        info.addStringPermission(byName.getPerms());
//        return info;
        return null;
    }


}
